package com.module.app.widget.dialog;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;

import androidx.interpolator.view.animation.FastOutSlowInInterpolator;

import com.module.app.R;


/**
 * @author bsnl_yanxuwen
 * @date 2021/1/21 14:58
 * Description :
 */
public class LoadingDialog extends BaseDialog {
    private Context context;
    private View loading;

    public LoadingDialog(Context context) {
        super(context, R.style.loading_style);
        this.context = context;
    }

    public LoadingDialog(Context context, int theme) {
        super(context, R.style.loading_style);
        this.context = context;
    }

    @Override
    public int getLayoutId() {
        return R.layout.app_dialog_loading;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //使用主题动画的时候，dismiss glide gif动画会失效
        //setAnimResId(R.style.showDialog);
        super.onCreate(savedInstanceState);
        loading = findViewById(R.id.loading);
        init();
    }

    private void init() {
        setCancelable(true);
        setCanceledOnTouchOutside(false);
    }

    public @interface DISMISS_STATUS {
        int normal = 0;//正常
        int ing = 1;//进行中
        int suc = 2;//成功
    }

    /**
     * 0 正常状态 1 正在关闭， 2 关闭成功
     */
    @DISMISS_STATUS
    int dismissStatus;

    @Override
    public void show() {
        super.show();
        loading.setVisibility(View.VISIBLE);
        rootView.setAlpha(1);
    }

    @Override
    public void dismiss() {
        if (DISMISS_STATUS.suc == dismissStatus) {
            dismissStatus = DISMISS_STATUS.normal;
            if (context != null && context instanceof Activity && ((Activity) context).isFinishing()) {
                //避免activity销毁
                return;
            }
            super.dismiss();
            return;
        }
        if (DISMISS_STATUS.ing == dismissStatus) {
            return;
        }
        dismissStatus = DISMISS_STATUS.ing;
        int duration = 500;
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(
//            ObjectAnimator.ofFloat(rootView, "scaleX", 1, 0.7f).setDuration(duration),
//            ObjectAnimator.ofFloat(rootView, "scaleY", 1, 0.7f).setDuration(duration),
            ObjectAnimator.ofFloat(rootView, "alpha", 1, 0).setDuration(duration)
        );
        animatorSet.setInterpolator(new FastOutSlowInInterpolator());
        animatorSet.start();
        animatorSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                dismissStatus = DISMISS_STATUS.suc;
                loading.setVisibility(View.GONE);
                dismiss();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
//    }
    }
}
